/**
 * 
 */
package com.taobao.snowsnow.taobao;

import java.sql.Timestamp;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;

import com.taobao.api.ApiException;
import com.taobao.api.request.ItemGetRequest;
import com.taobao.api.request.ItemUpdateRequest;
import com.taobao.api.request.ItemsOnsaleGetRequest;
import com.taobao.api.response.ItemGetResponse;
import com.taobao.api.response.ItemUpdateResponse;
import com.taobao.api.response.ItemsOnsaleGetResponse;
import com.taobao.snowsnow.hibernate.pojo.ProductCollect;
import com.taobao.snowsnow.hibernate.service.ColorService;
import com.taobao.snowsnow.hibernate.service.DimensionService;
import com.taobao.snowsnow.hibernate.service.ItemCollectService;
import com.taobao.snowsnow.util.ApplicationContextUtil;
import com.taobao.snowsnow.util.GB2Alpha;

/**
 * @author tux
 *
 */
public class ProductUnsalable extends Taobao {
	
	public ProductUnsalable() throws ApiException, DocumentException {
		getPage();
	}
	
	/**
	 * 获取一个分页的商品
	 * @throws ApiException 
	 * @throws DocumentException 
	 */
	private void getPage() throws ApiException, DocumentException {
		ItemsOnsaleGetRequest req = new ItemsOnsaleGetRequest();
		req.setFields("num_iid");

		// 每页大小
		req.setPageSize(40l);
		
		for(long i = 1; i <= 40l; i++) {
			// 页码
			req.setPageNo(i);
			
			// sessionKey
			ItemsOnsaleGetResponse response = client.execute(req, TaoBaoUtil.SESSIONKEY);

			getItems(response.getBody());
		}
		
		System.out.println("over");
	}
	
	/**
	 * 获取多个商品
	 * @param xmlBody
	 * @throws DocumentException 
	 * @throws ApiException 
	 */
	@SuppressWarnings("unchecked")
	public void getItems(String xmlBody) throws DocumentException, ApiException {
		Document document = DocumentHelper.parseText(xmlBody);

		List<DefaultElement> list = document.selectNodes("//items_onsale_get_response/items/item");
		
		for (DefaultElement df : list) {
			
			Element num_iid = df.element("num_iid");
			
			long iid = Long.valueOf(num_iid.getText());
//					System.out.println(iid);

			ItemGetRequest req1 = new ItemGetRequest();
			req1.setFields("num_iid, desc");
			req1.setNumIid(iid);

			ItemGetResponse response = client.execute(req1, TaoBaoUtil.SESSIONKEY);
			getItem(response.getBody());
//			System.out.println(i++);
		}
	}
	
	/**
	 * 获取单个商品
	 * @param xmlBody
	 * @throws DocumentException
	 * @throws ApiException 
	 * @throws NumberFormatException 
	 */
	@SuppressWarnings("unchecked")
	private void getItem(String xmlBody) throws DocumentException, NumberFormatException, ApiException {
		Document doc = DocumentHelper.parseText(xmlBody);
		List<DefaultElement> l = doc.selectNodes("//item_get_response/item");
		
		for (DefaultElement d : l) {
			
			String desc = d.element("desc").getText();
			String num_iid = d.element("num_iid").getText();
			
			updateItemDesc(Long.valueOf(num_iid), desc.replaceFirst("&nbsp;", "&nbsp;&nbsp;"));
			//			getSku(sku, split);
//			itemService.create(productCollect);
		}
	}
	
	private void updateItemDesc(long num_iid, String desc) throws ApiException {
		ItemUpdateRequest req=new ItemUpdateRequest();
		req.setNumIid(num_iid);
		req.setDesc(desc);
		ItemUpdateResponse response = client.execute(req , TaoBaoUtil.SESSIONKEY);
		System.out.println(response.getBody());
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			new ProductUnsalable();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
